﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MODULE_KETOAN
{
    public partial class frm_rpt_bangkethumua : Form
    {
        ketoanDataContext context = cls_ketoan.getcontext();
        public frm_rpt_bangkethumua()
        {
            InitializeComponent();
        }

        private void btPrint_Click(object sender, EventArgs e)
        {
           
            try
            {
                string ngaycan;
                string d, m, y;

                if (this.dtpngaycan.Value.Day >= 10)
                {
                    d = this.dtpngaycan.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtpngaycan.Value.Day.ToString();
                }

                if (this.dtpngaycan.Value.Month >= 10)
                {
                    m = this.dtpngaycan.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtpngaycan.Value.Month.ToString();
                }
                y = this.dtpngaycan.Value.Year.ToString();
                
               
                ngaycan = d + "/" + m + "/" + y;

                string connectstring = context.Connection.ConnectionString;
                SqlConnection connection = new SqlConnection(connectstring);
                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.CommandText = "KT_rpt_bangkethumuatrongngay'"+ngaycan+"'";

                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable tb = new DataTable();
                adapter.Fill(tb);
                //IEnumerable<KT_rpt_bangkethumuatrongngayResult> lst = context.KT_rpt_bangkethumuatrongngay(ngaycan).AsQueryable();




                MODULE_TIENICH.cls_report objReport = new MODULE_TIENICH.cls_report();


                Cursor.Current = Cursors.WaitCursor;
                objReport.Load_Report(Application.StartupPath + "\\reports\\bangkethumuatrongngay.rpt", tb);
                //objReport.Load_SubReport("Sub_BienBanNhap_VTNPL_BTP", objCtrl.LoadPrint_BienBanNhap_L)
                objReport.Formulas_Report("ngaycan", "'" + ngaycan + "'");
                objReport.Open_Report();
                Cursor.Current = Cursors.Default;
                //objCtrl = Nothing
                //objReport = Nothing
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btExcel_Click(object sender, EventArgs e)
        {
            string ngaycan;
            string d, m, y;
           
            this.saveFileDialog1.Filter = "Excel file(*.xls)|*.xls";

            this.saveFileDialog1.FileName = "bangkethumuatrongngay.xls";
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;
                if (this.dtpngaycan.Value.Day >= 10)
                {
                    d = this.dtpngaycan.Value.Day.ToString();
                }
                else
                {
                    d = "0" + this.dtpngaycan.Value.Day.ToString();
                }

                if (this.dtpngaycan.Value.Month >= 10)
                {
                    m = this.dtpngaycan.Value.Month.ToString();
                }
                else
                {
                    m = "0" + this.dtpngaycan.Value.Month.ToString();
                }
                y = this.dtpngaycan.Value.Year.ToString();


                ngaycan = d + "/" + m + "/" + y;

                List<KT_rpt_bangkethumuatrongngayResult> lst = context.KT_rpt_bangkethumuatrongngay(ngaycan).ToList();

                grdata.Rows.Clear();

                if (lst != null)
                {

                    for (int i = 0; i < lst.Count(); i++)
                    {
                        KT_rpt_bangkethumuatrongngayResult ele = lst.ElementAt(i);
                        grdata.Rows.Add();
                        grdata.Rows[i].Cells["sophieu"].Value = ele.sophieu;
                        grdata.Rows[i].Cells["tenchuhang"].Value = ele.tenchuhang;
                        grdata.Rows[i].Cells["diachi"].Value = ele.diachichuhang;
                        grdata.Rows[i].Cells["xecho"].Value = ele.xecho;
                        grdata.Rows[i].Cells["trongluongxoi"].Value = ele.trongluongxoi;
                        grdata.Rows[i].Cells["trongluongxe"].Value = ele.trongluongxe;
                        grdata.Rows[i].Cells["trongluongcan"].Value = ele.trongluongcan;
                        grdata.Rows[i].Cells["tenhang"].Value = ele.tenhang;
                        grdata.Rows[i].Cells["trongluongquychuan"].Value = ele.tlquychuan;
                        grdata.Rows[i].Cells["dongia"].Value = ele.dongia;
                        grdata.Rows[i].Cells["thanhtien"].Value = ele.thanhtien;

                    }
                }
                String filePath = saveFileDialog1.FileName;

                ExcelWrite myExcel = new ExcelWrite();
                List<String> list = myExcel.DGVToExcel(grdata);
                //String filePath = path + "\\BangKeThanhToan.xls";

                myExcel.WriteDateToExcel(filePath, list, "A1", "K1", "A2");
                MessageBox.Show("Export dữ liệu thành công ...", "Thông báo");
                Cursor.Current = Cursors.Default;
            }    


        }

        private void frm_rpt_bangkethumua_Load(object sender, EventArgs e)
        {
         

        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
    }
}
